<template>
    <el-row>
        <el-col :span="6" class="scrollDiv" style="height:calc(100vh - 90px);overflow-y: scroll">
        <CommonPmsCategory @queryData= "queryData"/>
        </el-col>
        <el-col :span="18" >
            <el-form inline >
                <el-form-item label="用户名">
                    <el-input v-model="query.username" placeholder="用户名" />
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="query.gender" placeholder="性别">
                        <MySelectOption typeCode = "gender"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" plain  activity @click="queryData">查询</el-button>
                    <el-button type="primary" plain  activity @click="reset">重置</el-button>
                    <el-button type="primary" plain  @click="add">新增</el-button>
                </el-form-item>
            </el-form>
            <el-button type="primary" size="small" @click="importUsers" v-if="authBtn.hasBtnAuth('/user/importUsers')">
                导入<el-icon class="el-icon--right"><Upload/></el-icon>
            </el-button>
            <el-divider style="margin: 12px 0"/>
            <el-table ref="table" border  v-loading="loading"  :data="data.tableData" header-cell-class-name="headerClass">
                <el-table-column align="center" type="index"  label="序号" width="100px" />
                <el-table-column align="center"  prop="username" label="用户名" />
                <el-table-column align="center"  prop="gender" label="性别">
                    <template v-cloak #default="scope">
                        {{transDicValue(genderDicItems, scope.row.gender)}}
                    </template>
                </el-table-column>
                <el-table-column align="center"  prop="nickName" label="昵称" />
                <el-table-column align="center"  prop="address" show-overflow-tooltip label="地址" />
                <el-table-column align="center"  prop="orgName" label="机构" />
                <el-table-column align="center"  prop="updateTime" label="更新时间" >
                    <template #default="scope">
                        {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
                    </template>
                </el-table-column>
                <el-table-column align="center"  label="操作">
                    <template #default="scope">
                        <el-link style="font-size: 14px" title="配置" type="primary" @click="editRoleOnUser(scope.row.id)">
                            <el-icon :size="15">
                                <Setting />
                            </el-icon>
                        </el-link>
                        &nbsp;
                        <el-link style="font-size: 14px" title="修改" type="primary" @click="toEdit(scope.row.id)">
                            <el-icon :size="15">
                                <Edit />
                            </el-icon>
                        </el-link>
                        &nbsp;
                        <el-popconfirm title="删除？" @confirm="toDelete(scope.row.id)"  @cancel="">
                            <template #reference>
                                <el-link style="font-size: 14px" type="danger" title="删除">
                                    <el-icon :size="15">
                                        <Delete />
                                    </el-icon>
                                </el-link>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    style="display: flex;align-items:center;justify-content:center;"
                    v-model:currentPage="query.currentPage"
                    v-model:page-size="query.pageSize"
                    :page-sizes="[10, 20, 30]"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="query.total"
                    @size-change="queryData"
                    @current-change="queryData"
            />
        </el-col>
    </el-row>
</template>

<script setup>
    import ModUser from '@/components/system/ModUser.vue'
    import RoleOnUser from '@/components/system/RoleOnUser.vue'
    import MySelectOption from '@/components/common/MySelectOption.vue'
    import ImportDataDialogue from '@/components/common/ImportDataDialogue.vue'
    import CommonPmsCategory from '@/components/common/CommonPmsCategory.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref,  reactive, onMounted} from 'vue'
    import moment from 'moment'

    import useAuthBtn from '@/hooks/useAuthBtn.js'
    let authBtn = useAuthBtn()

    let data = reactive({
        open:false,
        tableData:[],
        type:"",
        id:"",
    });
    let loading = ref(true)
    let roleOnUser = ref(null)
    let modUser = ref(null)
    let importDataDialogue = ref(null)
    let genderDicItems = reactive([])

    let query = reactive({currentPage:1, pageSize:10, total:0, payload: null})
    onMounted(() => {
        loading.value = false
    })
    function queryData(groupId){
        query.payload = groupId
        loading.value = true
        request.post("/api/pms-attr-group/getAttrGroupsByCateId", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }
    function reset (){
        query.username = ""
        query.gender = ""
    }
    function transDicValue (dicItmes, code){
        let value = '-'
        for (let i = 0; i < dicItmes.length; i++) {
            if(dicItmes[i].code == code){
                value = dicItmes[i].value
                break
            }
        }
        return value
    }

    function toAdd(){
        modUser.value.showDialog = true
        data.type = "add"
    }
    function toDelete(id){
        request.get(`/api/user/delete/${id}`).then(resp => {
            queryData()
            ElMessage({message: '删除成功！',type: 'success'})
        });
    }
    function toEdit(id){
        data.id = id
        data.type = "edit"
        modUser.value.showDialog = true
    }

</script>
<style lang="css" scoped>
    .address {overflow:hidden;white-space:nowrap;word-wrap:break-word;text-overflow:"ellipsis";}
    .head {background-color: #9561cc}
    [v-cloak]{
        display: none  !important;
    }
    .demo-form-inline{
        display: flex !important;
        justify-content: flex-start !important;
        align-items: center !important;
    }
    </style>